Conversion Trivia: Using a Listbox

level: beginner

© 2005, [Welopez]

author contact:

Welopez


Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index

Recently I received a very humorous email which I wanted to share with my Internet chums. I have so much fun with Liberty Basic, I wanted to set up a display which would be more entertaining than simply forwarding an email, so I wrote this program for Conversion Trivia and decided to use a LISTBOX to display choices to the user. A LISTBOX certainly seemed more appropriate than a window with 28 buttons, radiobuttons, or checkboxes. If you've never created a LISTBOX before, I'm sure you will agree it was the best choice and is not very difficult.

A LISTBOX displays text to the user; being text, all listed items are strings. The items might be links to files for the players of your favorite football team, images or sounds to be displayed, or a question and answer quiz such as this one. A LISTBOX might also contain names for your telephone directory, or items in your collection of antique beer bottles. When you add or delete people from your telephone list, or trade one of your beer bottles, you will have to RELOAD your LISTBOX to reflect the current data. Somewhere in a galaxy far, far away, using your choice of space vehicles from our Space Probes demos, we'll discuss RELOADing the LISTBOX.

Here is the code for my demo program.


'Conversion-Trivia for Geeks
'Written by Welo, 022605

NOMAINWIN

WindowWidth=420
WindowHeight=175
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)

BUTTON #1.btn1, "NEXT", newWin, UL, 175, 115, 65, 20
GRAPHICBOX #1.gbx, 5, 10, 400, 90

OPEN "How to use this program" FOR window AS #1
PRINT #1, "trapclose [newWin]"
PRINT #1.gbx, "backcolor buttonface"
PRINT #1.gbx, "place 10 15"
'PRINT #1.gbx, "flush"

ex$="The computer age and the Internet have given us new \"+ _
"terms to become familiar with.  From the following list, \"+ _
"please click on a topic to learn the proper conversion factor.\\"+ _
"Click NEXT to continue."
PRINT #1.gbx, "\"; ex$

WAIT
[newWin]
    CLOSE #1

DIM arr$(28), modTerm$(28)

    FOR cntr=1 to 28
        READ temp1$, temp2$
        arr$(cntr) = temp1$
        modTerm$(cntr) = temp2$
    NEXT cntr

WindowWidth=520
WindowHeight=290
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)

LISTBOX #2.lbx, arr$(), [ansWin], 10, 10, 500, 200
BUTTON #2.btn, "QUIT", [quit], UL, 225, 225, 65, 20
OPEN "Traditional English Units" FOR window AS #2
PRINT #2, "trapclose [quit]"

[newChoice]  'Return here when gag window closed.
PRINT #2.lbx, "setfocus"
PRINT #2.lbx, "font arial 12"
PRINT #2.lbx, "singleclickselect"

WAIT
[ansWin]  'Display modern term.
PRINT #2.lbx, "selection? selected$"
PRINT #2.lbx, "selectionindex? newTemp"
answer$= selected$+CHR$(13)+CHR$(13)+ _
"                    is equivalent to..."+ _
CHR$(13)+CHR$(13)+modTerm$(newTemp)

WindowWidth=300
WindowHeight=180
UpperLeftX=100
UpperLeftY=100

BUTTON #gag.btn, "CLOSE", [closeGag], UL, 130, 125, 40, 20
STATICTEXT #gag.stxt,"", 10, 5, 280, 120

OPEN "The Conversion Factor is..." FOR dialog_modal AS #gag
PRINT #gag, "trapclose [closeGag]"
PRINT #gag.stxt, "!font arial 12 bold"
PRINT #gag.stxt, answer$

WAIT
[closeGag]
    CLOSE #gag : GOTO [newChoice]

WAIT
[quit]
    CLOSE #2

END

DATA "Ratio of an igloo's radius to it's circumference.", "2 Eskimo pi."
DATA "2000 pounds of Chinese soup.", "1 Won Ton."
DATA "1 millionth of a mouthwash.", "1 microscope."
DATA "Time between slipping on a peel and whacking the pavement.", "1 bananosecond."
DATA "Weight an evangelist carries with God.", "1 billigram."
DATA "Time it takes to sail 220 yards at 1 knot.", "Knotfurlong."
DATA "365.25 days of drinking low calorie beer.", "1 Lite Year."
DATA "16.5 feet in the Twilight Zone.", "1 Rod Serling."
DATA "Half a large intestine.", "1 semicolon."
DATA "1 million aches.", "1 megahurtz."
DATA "Basic unit of laryngitis.", "1 hoarsepower."
DATA "Shortest distance between two jokes.", "1 straight line."
DATA "453.6 graham crackers.", "1 pound cake."
DATA "1 million microphones.", "1 megaphone."
DATA "1 million bicycles.", "1 megacycle."
DATA "365.25 days.", "1 unicycle."
DATA "2000 mocking birds.", "2 kilomockingbirds."
DATA "10 cards.", "1 decacard."
DATA "52 cards.", "1 dekacards."
DATA "1 kilogram of falling figs.", "1 figNewton."
DATA "1000 grams of wet socks.", "1 literhosen."
DATA "1 millionth of a fish.", "1 microfish."
DATA "1 trillion pins.", "1 terrapin."
DATA "10 rations.", "1 decoration."
DATA "100 rations.", "1 C-ration."
DATA "2 monograms.", "1 diagram."
DATA "8 nickels.", "2 paradigms."
DATA "2.4 statute miles of surgical tubing at Yale Medical School.", "1 I.V. league."
DATA "thisIsTheEnd.", "noMoreData."

By now, you all know the basic procedures to create and position a GUI on your screen, so we'll skip all that boring WindowWidth and WindowHeight stuff. The first thing we do in this program is tell the user the purpose of this program and what is expected, using a short message and a button to begin.

Following that, our code reads the DATA statements for prompts and answers to be loaded into arrays. An array is needed to hold the items which will be displayed in our list box. Remember, when loading an array from DATA statements within the program, all elements must first be read to a temporary variable before being assigned to the array. In this demo, we need two arrays, one for the LISTBOX items, and one for the humorous responses.

The LISTBOX is a control to be placed on the next window we open. The syntax is:


LISTBOX #2.lbx, arr$(), [ansWin], 10, 10, 500, 200
BUTTON #2.btn, "QUIT", [quit], UL, 225, 225, 65, 20

Your code may be slightly different. My LISTBOX will be placed on window #2 and have a control extension of .lbx. The items to be displayed in the LISTBOX are stored in arr$(), with no variable inside the parenthesis. When any item in the LISTBOX is clicked, it will branch to [ansWin]. The location of the LISTBOX on window #2 is 10 pixels right and 10 pixels down from the UL corner, and the LISTBOX will be 500 pixels wide by 200 pixels high. That wasn't difficult, was it?

Now that we have specified the controls, a LISTBOX and a BUTTON, for display on #2, we can OPEN window #2, add the trapclose statement, and specify the font for use within the LISTBOX. When we branch to [newWin], we also close #1 to prevent a run-time error if #1 is still OPEN when we eventually terminate this program. It's nice to be able to close windows no longer needed from elsewhere in a program. We can also OPEN them again without encountering a run-time error happening if the window is already OPEN.

Unless your code has ESP built-in, you must WAIT for the user to make a selection from the list. When the user clicks on an item in the LISTBOX, execution branches to the [ansWin] as specified for the LISTBOX control. You can require single-click or double-click when writing your program. LB uses double-click as the default, unless you specify "singleclickselect" to branch to the click-event.

There are several variables we can get from the LISTBOX. See the helpfile LISTBOX topic for a complete list. In this demo we use only "selection? selected$", which returns the string of the item selected, and "selectionindex? newTemp", which is the index of the item within our list of 28 items and points to the element number of our humorous response array.

Okay, you wrote your beautiful code, but your code ignores the selection item and displays the [ansWin] with no prompt, no answer, and you can't close the #%$#!& thing! Did you remember to precede the "selection? selected$" with a WAIT statement? Without WAIT, program execution continues in a straight line, just as if you entered nothing. Regardless of how fast you are with a mouse, the computer is faster! You must WAIT for a user response before your code continues.

When the user single-clicks on an item in this list, the selected$ is set as a variable to be displayed when [ansWin] is OPENed, and selectionindex is set as newTemp. Answer$ is built from selected$+" is equivalent to..."+modTerm$(newTemp), with a couple line feeds thrown in for vertical spacing.

Finally we OPEN [ansWin] where answer$ is displayed to the user, hopefully eliciting a chuckle or two. [ansWin] is OPENed FOR dialog_modal, which means nothing more can happen in the program until the user closes the [ansWin]. Because [ansWin] is modal, our program is 'bullet-proofed' against unwanted fiddling by the user.

Now, wasn't that fun?


Home

ActiveX DLLs 2

API Trackbar/Slider

OOP

Stylebits - Textboxes

Using a ListBox

Real 3-D

Context Sensitive Help

Program Design

Texteditors

Tip Corner - Nomainwin

API Corner - MainWin

Sprite Byte: Scoreboard

Eddie

Newsletter help

Index